#include "leaderboardlogic.h"
#include <string.h>
#include "../data/sort.h"

void leaderboardInit(Leaderboard* lb) {
    lb->count = 0;
}

void leaderboardAdd(Leaderboard* lb, const char* name, int score) {
    if (lb->count < MAX_PLAYERS) {
        strncpy(lb->players[lb->count].name, name, NAME_LEN-1);
        lb->players[lb->count].name[NAME_LEN-1] = '\0';
        lb->players[lb->count].score = score;
        lb->count++;
    }
}

// 比较函数：按分数降序
int leaderboardCmpScoreDesc(const void* a, const void* b) {
    const Player* pa = (const Player*)a;
    const Player* pb = (const Player*)b;
    return pb->score - pa->score;
}

// 排序接口示例
void leaderboardSortByScore(Leaderboard* lb) {
    sort_bubble(lb->players, lb->count, sizeof(Player), leaderboardCmpScoreDesc);
} 